/**
 * Copyright (c) 2020 qar All rights reserved.
 * <p>
 * https://www.qar.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.example.controller;

import com.example.dto.GenTemplateDTO;
import com.example.entity.BaseClassEntity;
import com.example.exception.BaseException;
import com.example.model.page.PageData;
import com.example.model.api.Result;
import com.example.entity.TemplateEntity;
import com.example.service.TemplateService;
import com.example.utils.ConvertUtils;
import com.example.utils.GenUtils;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("template")
public class TemplateController {
    @Autowired
    private TemplateService templateService;

    @GetMapping("page")
    public Result<PageData<GenTemplateDTO>> page(@RequestParam Map<String, Object> params){
        PageData<GenTemplateDTO> page = templateService.page(params);

        return new Result<PageData<GenTemplateDTO>>().ok(page);
    }

    @GetMapping("{id}")
    public Result<GenTemplateDTO> get(@PathVariable("id") Long id){
        TemplateEntity data = templateService.selectById(id);
        return new Result<GenTemplateDTO>().ok(ConvertUtils.sourceToTarget(data, GenTemplateDTO.class));
    }

    @PostMapping
    public Result<String> save(@RequestBody GenTemplateDTO genTemplateDTO){
        ValidatorUtils.validateEntity(genTemplateDTO, AddGroup.class, DefaultGroup.class);
        TemplateEntity entity = ConvertUtils.sourceToTarget(genTemplateDTO, TemplateEntity.class);
        try {
            //检查模板语法是否正确
            GenUtils.getTemplateContent(entity.getContent(), new HashMap<>());
        } catch (Exception e) {
            throw new BaseException("模板语法错误，请查看控制台报错信息！");
        }

        templateService.insert(entity);

        return new Result<String>().ok("ok");
    }

    @PutMapping
    public Result<String> update(@RequestBody GenTemplateDTO genTemplateDTO){
        ValidatorUtils.validateEntity(genTemplateDTO, UpdateGroup.class, DefaultGroup.class);
        TemplateEntity entity = ConvertUtils.sourceToTarget(genTemplateDTO, TemplateEntity.class);
        try {
            //检查模板语法是否正确
            GenUtils.getTemplateContent(entity.getContent(), new HashMap<>());
        } catch (Exception e) {
            throw new BaseException("模板语法错误，请查看控制台报错信息！");
        }
        templateService.updateById(entity);

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    public Result<String> delete(@RequestBody Long[] ids){
        AssertUtils.isListEmpty( Arrays.asList(ids), "不能为空");
        templateService.deleteBatchIds(Arrays.asList(ids));

        return new Result<String>().ok("ok");
    }

    /**
     * 启用
     */
    @PutMapping("enabled")
    public Result<String> enabled(@RequestBody Long[] ids){
        templateService.updateStatusBatch(ids, 0);

        return new Result<String>().ok("ok");
    }

    /**
     * 禁用
     */
    @PutMapping("disabled")
    public Result<String> disabled(@RequestBody Long[] ids){
        templateService.updateStatusBatch(ids, 1);

        return new Result<String>().ok("ok");
    }
}